using System.Collections.Generic;
using IRepository;
using IServices;
using SqlSugar;
using YL.Core.Dto;
using YL.Core.Entity;
using YL.Utils.Extensions;
using YL.Utils.Table;

namespace Services
{
    public class Whr_notificationServices : BaseServices<Whr_notification>, IWhr_notificationServices
    {
        private readonly SqlSugarClient _client;

        private readonly IWhr_notificationRepository _notificationRepository;

        public Whr_notificationServices(SqlSugarClient client, IWhr_notificationRepository repository) : base(repository)
        {
            _client = client;
            _notificationRepository = repository;
        }

        /// <summary>
        /// 通知列表
        /// </summary>
        /// <param name="userId"></param>
        /// <param name="categoryId"></param>
        /// <param name="bootstrap"></param>
        /// <returns></returns>
        public (int, List<WhrNotification>) PageList(int userId, int categoryId, Bootstrap.BootstrapParams bootstrap)
        {
            var totalNumber = 0;
            var query = _client.Queryable<Whr_notification_recipient, Whr_notification>((r, n) => new object[]
            {
                JoinType.Left,
                r.NotificationId==n.NotificationId
            });
            List<WhrNotification> result = query.Where(
                    (r, n) => 
                    r.UserId == userId
                    && r.CategoryId==categoryId
                    && n.IfSchedule==1
                    && SqlFunc.IsNullOrEmpty(n.CancelTime)
                    && SqlFunc.IsNullOrEmpty(n.DeleteTime)
                )
                .Select<WhrNotification>()
                .ToPageList(bootstrap.offset, bootstrap.limit, ref totalNumber);
            return (totalNumber, result);
        }

        /// <summary>
        /// 通知详情
        /// </summary>
        /// <param name="userId"></param>
        /// <param name="NotificationId"></param>
        /// <returns></returns>
        public WhrNotification Find(int userId, int NotificationId)
        {
            var query = _client.Queryable<Whr_notification_recipient, Whr_notification>((r, n) => new object[]
            {
                JoinType.Left,
                r.NotificationId==n.NotificationId
            });
            WhrNotification result = query.Where(
                    (r, n) =>
                        r.UserId == userId
                        && r.NotificationId == NotificationId
                        && n.IfSchedule == 1
                        && SqlFunc.IsNullOrEmpty(n.CancelTime)
                        && SqlFunc.IsNullOrEmpty(n.DeleteTime)
                )
                .Select<WhrNotification>()
                .First();
            return result;
        }

        public void Read(int userId, int notificationId)
        {
            _client.Updateable<Whr_notification_recipient>()
                .Where(r => r.UserId == userId && r.NotificationId == notificationId).SetColumns(r=>r.ReadTime==DateTimeExt.GetUnixTimestamp());
        }
    }
}